package com.bjtg.threegoclinet.util;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;

import static com.bjtg.threegoclinet.MainActivity.isNetExist;

/**
 * Created by 86463 on 2017/12/6.
 */

public class IsInternet {

    /**
     * 没有连接网络
     */
    public static final int NETWORK_NONE = -1;
    /**
     * 移动网络
     */
    public static final int NETWORK_MOBILE = 0;
    /**
     * 无线网络
     */
    public static final int NETWORK_WIFI = 1;

    public static final String NETWORK_NONE_TEXT = "当前网络或服务异常，请稍候！";
    private static ConnectivityManager connectivityManager;
    private static NetworkInfo activeNetworkInfo;

    public static int getNetWorkState(Context context) {
        // 得到连接管理器对象
        if (connectivityManager == null) {
            connectivityManager = (ConnectivityManager) context
                    .getSystemService(Context.CONNECTIVITY_SERVICE);

            activeNetworkInfo = connectivityManager
                    .getActiveNetworkInfo();
        }
        if (activeNetworkInfo != null && activeNetworkInfo.isConnected()) {

            if (activeNetworkInfo.getType() == (ConnectivityManager.TYPE_WIFI)) {
                return NETWORK_WIFI;
            } else if (activeNetworkInfo.getType() == (ConnectivityManager.TYPE_MOBILE)) {
                return NETWORK_MOBILE;
            }
        } else {
            return NETWORK_NONE;
        }
        return NETWORK_NONE;
    }

    // 如果没有网络，则弹出网络设置对话框
    public static boolean checkNetwork(final Context context) {
        if (IsInternet.getNetWorkState(context) == NETWORK_NONE) {
            isNetExist = false;
//            BaiduTTSSpeakUtil.getInstance().speak("当前没有可以使用的网络，请设置网络！");
        } else {
            isNetExist = true;
            //网络连接成功，欢迎您使用ThreeGo
//            BaiduTTSSpeakUtil.getInstance().speak("网络连接成功，欢迎您使用ThreeGo");
        }
//        JpushSharedPreferences.initAppFunction(MyApplication.getApplicationMyContext());
        return isNetExist;
    }
}
